"""
JAKE ELLOWITZ
"""

import random
from numpy import e
from vec2d import vec

class share_policy:
  def __init__ (self):
    """
    """
    pass

  def check_touching_bots_share (self, bots, bot, bot_radius, search_radius):
    """
    Used for checking the bots which to possibly share policies with
    Construct a list of share_bots which bot will potentially share
    its policy with. Note that the other bot will then broadcast its 
    policy, so it is important NOT to check both ways.
    """
    share_bots = []
    for nearby_bot in bots:
      d = vec(nearby_bot.x - bot.x, nearby_bot.y - bot.y)
      if d.mag_sq () < (bot_radius + search_radius)**2\
      and nearby_bot != bot\
      and nearby_bot not in share_bots:
        share_bots.append (nearby_bot)
    return share_bots

  def policy_share_test (self, bot, other_bot, p):
    # Update the value functions
    bot.bra.v = bot.bra.update_vs (bot.bra.n_states, bot.bra.q, bot.bra.v)
    other_bot.bra.v = other_bot.bra.update_vs (other_bot.bra.n_states, other_bot.bra.q, other_bot.bra.v)
    val = sum (bot.bra.v)
    other_val = sum (other_bot.bra.v)
    if val > other_val and random.random() < p:
      return True
    else:
      return False

